package com.example.a14110100409.yuejiandemo.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.javabean.User;
import com.example.a14110100409.yuejiandemo.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class RegisterActivity extends AppCompatActivity {
    private EditText telephone,password,passwordagain;
    private Button btn_reg;
    private Button btn_return;
    private  String error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();

    }
    public void initEvents() {
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent1);
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_psd =password.getText().toString();
                String str_psdagain=passwordagain.getText().toString();
                String str_tel=telephone.getText().toString();

                if(str_psd.equals("") || str_tel.equals("")){
                    Toast.makeText(RegisterActivity.this, "您有的信息还未填写", Toast.LENGTH_SHORT).show();
                }else{
                    if(str_psd.equals(str_psdagain)){
                        UserModel userModel = new UserModel();
                        Call<User> call = userModel.register(str_tel,str_psd);
                        //3. 发送请求
                        call.enqueue(new Callback<User>() {
                            @Override
                            public void onResponse(Call<User> call, Response<User> response) {
                                error = response.body().getError();
                                if(!"手机号已被使用".equals(error)){
                                    Toast.makeText(getApplication(),"注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    String str_user=telephone.getText().toString();
                                    intent.putExtra("username",str_user);
                                    setResult(RESULT_OK,intent);
                                    finish();
                                } else{
                                    Toast.makeText(RegisterActivity.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onFailure(Call<User> call, Throwable t) {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        });



                        /*call.enqueue(new Callback<User>(){
                            @Override
                            public void onFailure(Call<User> uname, Throwable throwable) {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                            @Override
                            public void onResponse(Call<User> uname, Response<User> response) {
                                error = response.body().getError();
                                if("手机号已被使用".equals(error)){
                                    Toast.makeText(RegisterActivity.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
                                } else{
                                    Toast.makeText(getApplication(),"注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    String str_user=telephone.getText().toString();
                                    intent.putExtra("username",str_user);
                                    setResult(RESULT_OK,intent);
                                    finish();
                                }
                            }
                        });*/
                    }
                    else{
                        Toast.makeText(RegisterActivity.this, "两次密码输入不同请确认", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private void initViews() {
        password= (EditText) findViewById(R.id.password);
        passwordagain= (EditText) findViewById(R.id.passwordagain);
        telephone= (EditText) findViewById(R.id.tel);
        btn_reg= (Button) findViewById(R.id.button3);
        btn_return= (Button) findViewById(R.id.button4);
    }


}
